class Caption{
  cavWidth = 100
  width = 0
  top = 12
  left = 0
  topIndex = 0
  isMove = true
  occupyPos = true
  isOver = false
  speed = 1
  color = ''
  content = ''
  latestTime = 0
  static topObjs = [
    {blank:true , value : 12 ,index:0},
    {blank:true , value : 26 ,index:1},
    {blank:true , value : 40 ,index:2},
    {blank:true , value : 53 ,index:3}
  ]

  constructor(ismove , spe , col , text,cavWidth){
    this.cavWidth = cavWidth
    this.isMove = ismove
    this.speed = spe
    this.color = col || "#ff0"
    this.content = text
    this.latestTime = 0
    this.width = text.length * 10
    this.topIndex = 0
    this.occupyPos = true
    this.isOver = false
    this.top = 12
    this.left = 0
    this.setLeftValue()
    this.setTopValue()
  }

  setTopValue = function  () {
    for(var i = 0 ,len = Caption.topObjs.length ; i < len ; i++){
      if (Caption.topObjs[i].blank) {
        this.top = Caption.topObjs[i].value
        this.topIndex = i
        Caption.topObjs[i].blank = false
        return
      }
    }
    this.isOver = true
  }

  setLeftValue = function  () {
    if (this.isMove) {
      this.left = this.cavWidth
    }
    else {
      var contentLength = this.content.length
      var nowItemLeft = 420 - contentLength * 9
      this.left = nowItemLeft
    }
  }
  moving = function () {
    if (this.isMove) {
      this.left-=this.speed
      //是否需要独占一行
      if ( this.left + this.width < this.cavWidth && this.occupyPos) {
          //console.log("go to ")
          this.occupyPos = false
        Caption.topObjs[this.topIndex].blank = true
      }
    }
    else{
      this.latestTime += 1
      if (this.latestTime > 450) {
        Caption.topObjs[this.topIndex].blank = true
      }
    }
  }
}

export {Caption}
